package org.java.user.controller;

import org.java.commons.vo.Result;
import org.java.user.api.domain.User;
import org.java.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/registry")
public class RegistryController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String registry(){
        return "registry";
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView registry(User user , String password2){
        Result result = this.registry2( user,password2);
        ModelAndView mav = new ModelAndView();
        mav.addObject("result",result);
        if (result.getCode() == 1){
            mav.setViewName("success");
        }else {
            mav.setViewName("registry");
        }
        return mav;
    }

    @RequestMapping(method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Result registry2(User user,String password2){
        Result result ;
        if (StringUtils.isEmpty(password2)){
            result = Result.error("请在【确认密码】框中再次输入相同的密码");
        }else  if (!password2.equals(user.getPassword())){
            result = Result.error("请在【确认密码】框中再次输入相同的密码");
        }else {
            result = userService.registry(user);
        }
        return result;
    }
}
